package com.jt.tenant.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.MaintainShopAdapter;
import com.jt.tenant.adapter.MaintainTypeAdapter;
import com.jt.tenant.adapter.ShopCarAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetProductQuery;
import com.jt.tenant.widget.ShopCount;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MaintainShopActivity extends JTActivity implements View.OnClickListener , MaintainShopAdapter.OnAddShopList,ShopCarAdapter.OnChangeNum{

    private TextView mTitleText;

    private String name,address,phone,lat,lon,id,img,money;

    private TextView mMaintainName,mMaintainTel,mMaintainAddress;

    private ImageView mMaintainImg;

    private ImageView mShopCarImg;

    private LinearLayout mNavLin;

    private ListView mTypeListView;

    private MaintainTypeAdapter mTypeAdapter;

    private ListView mShopListView;

    private MaintainShopAdapter mShopAdapter;

    private List<GetProductQuery.GoodsList> mList = new ArrayList<>();

    private List<GetProductQuery.NameList> mNameList = new ArrayList<>();

    private List<GetProductQuery.NameList> mShopList = new ArrayList<>();

    private LinearLayout mPayLin;

    private LinearLayout mShoppinpCartLeft;

    private TextView mMoneyText;

    private TextView tv_confirm;

    private LinearLayout mBottomLin;

    private ListView mShopCarListView;

    private ShopCarAdapter mShopCarAdapter;

    private RelativeLayout mRel;

    private boolean mShopCarIsShow = false;

    private View mBlackView;

    private LinearLayout mClearLin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_maintain_shop);

        getData();

        initView();
    }

    private void getData(){

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        id = bundle.getString("id");


    }

    private void initView(){

        mTitleText = (TextView) findViewById(R.id.tv_title_name);

        mTitleText.setText("");

        mBlackView = (View)findViewById(R.id.view_black);

        mBlackView.setOnClickListener(this);

        mClearLin = (LinearLayout)findViewById(R.id.lin_clear);

        mClearLin.setOnClickListener(this);

        mRel = (RelativeLayout) findViewById(R.id.lin_shop_list) ;

        mBottomLin = (LinearLayout) findViewById(R.id.lin_maintain_bottom) ;

        mMoneyText = (TextView) findViewById(R.id.text_maintian_money);

        tv_confirm = (TextView)findViewById(R.id.tv_shopping_cart_confirm);

        mShopCarImg = (ImageView) findViewById(R.id.icon_maintain_car);

        mShopCarListView = (ListView)findViewById(R.id.list_shoplist);

        mShopCarAdapter = new ShopCarAdapter(this,mShopList,this);

        mShopCarListView.setAdapter(mShopCarAdapter);

        mShopCarImg.setOnClickListener(this);

        mShoppinpCartLeft = (LinearLayout)findViewById(R.id.lin_shopping_cart_left);

        mPayLin = (LinearLayout) findViewById(R.id.lin_pay);

        mPayLin.setOnClickListener(this);

        mMaintainName = (TextView) findViewById(R.id.text_maintain_name);

        mMaintainTel = (TextView) findViewById(R.id.text_maintain_tel);

        mMaintainImg = (ImageView) findViewById(R.id.img_maintain_shop);

        mMaintainAddress = (TextView) findViewById(R.id.text_maintain_address);

        mNavLin = (LinearLayout) findViewById(R.id.lin_nav);

        mNavLin.setOnClickListener(this);

        mTypeListView = (ListView) findViewById(R.id.list_maintain_type);

        mTypeAdapter = new MaintainTypeAdapter(this,mList);

        mTypeListView.setAdapter(mTypeAdapter);

        mShopListView = (ListView) findViewById(R.id.list_maintain_shop);

        mShopAdapter = new MaintainShopAdapter(this,mNameList,this);

        mShopListView.setAdapter(mShopAdapter);

        mTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for(int i=0;i<mList.size();i++){
                    mList.get(i).isSelect = false;
                }

                mList.get(position).isSelect = true;

                mTypeAdapter.notifyDataSetChanged();

                mNameList.clear();

                mNameList.addAll(mList.get(position).nameLists);

                mShopAdapter.notifyDataSetChanged();
            }
        });

        new GetProductQuery(id,new AsyCallBack<GetProductQuery.Info>(){

            @Override
            public void onSuccess(String toast, int type, GetProductQuery.Info info) throws Exception {
                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")) {

                    mMaintainName.setText(info.name);

                    mMaintainTel.setText("商家电话:"+info.phone);

                    JTApplication.UtilAsyncBitmap.get(info.logo,mMaintainImg);

                    mMaintainAddress.setText("地址 : "+info.address);

                    img = info.logo;

                    name = info.name;

                    address = info.address;

                    phone = info.phone;

                    lat = info.lat;

                    lon = info.lon;

                    mList.clear();

                    mList.addAll(info.list1);

                    mTypeAdapter.notifyDataSetChanged();

                    mNameList.clear();

                    mNameList.addAll(info.list1.get(0).nameLists);

                    mShopAdapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);


            }
        }).execute(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.lin_nav:

                startActivity(new Intent(context, BasicNaviActivity.class)

                        .putExtra("mLat", Double.parseDouble(JTApplication.lat))

                        .putExtra("mLon", Double.parseDouble(JTApplication.lon))

                        .putExtra("goLat",Double.parseDouble(lat))

                        .putExtra("goLon",Double.parseDouble(lon)));

                break;

            case R.id.icon_maintain_car:

                if(mShopList.size()>0){
                    if(mShopCarIsShow){

                        mShopCarIsShow = false;

                        mRel.setVisibility(View.GONE);

                    }else{

                        mShopCarIsShow = true;

                        mRel.setVisibility(View.VISIBLE);

                        mShopCarAdapter.notifyDataSetChanged();

                        setHeight();

                    }
                }else{

                    UtilToast.show(MaintainShopActivity.this,"购物车是空的");

                }

                break;
            case R.id.view_black:

                if(mShopCarIsShow){

                    mShopCarIsShow = false;

                    mRel.setVisibility(View.GONE);

                }

                break;

            case R.id.lin_clear:

                mShopList.clear();

                setPayMoney();

                break;

            case R.id.lin_pay:

                if(mShopList.size()==0){

                    UtilToast.show(MaintainShopActivity.this,"购物车是空的");

                }else{

                    mShopCarIsShow = false;

                    mRel.setVisibility(View.GONE);

                    Intent intent = new Intent(MaintainShopActivity.this,OrderSureActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name",name);
                    bundle.putString("phone",phone);
                    bundle.putString("img",img);
                    bundle.putString("id",id);
                    bundle.putString("money",money);
                    bundle.putSerializable("list",(Serializable) mShopList);
                    intent.putExtras(bundle);
                    startActivity(intent);

                }

                break;

        }
    }
   //商品列表的加号监听回调
    @Override
    public void addShopList(GetProductQuery.NameList nameList) {

        boolean isHave = false;

        int num = 0;

        double money = 0;

        for(int i=0;i<mShopList.size();i++){

            num += mShopList.get(i).NUM;

            if(mShopList.get(i).ID.equals(nameList.ID)){

                 isHave = true;

                mShopList.get(i).NUM++;

            }

            money += Double.parseDouble(mShopList.get(i).PRICE)*mShopList.get(i).NUM;

        }

        if(!isHave){
            nameList.NUM = 1;

            money += Double.parseDouble(nameList.PRICE);

            mShopList.add(nameList);

        }

        num++;

        ShopCount.SendCount(MaintainShopActivity.this,num);

        mShopCarImg.setBackgroundResource(R.mipmap.icon_maintain_car_white);

        mPayLin.setBackgroundColor(getResources().getColor(R.color.ScollColor1));

        mShoppinpCartLeft.setBackgroundColor(getResources().getColor(R.color.MainColor));

        tv_confirm.setTextColor(Color.parseColor("#ffffff"));

        mMoneyText.setVisibility(View.VISIBLE);

        this.money = money + "";

        mMoneyText.setText("共计"+Tools.F_num(this.money) + "元");

        mShopCarAdapter.notifyDataSetChanged();

    }

    //购物车商品列表的加减号监听
    @Override
    public void onChangeNum(String id, boolean flag) {

        for (int i=0;i<mShopList.size();i++){

            if(mShopList.get(i).ID.equals(id)){

                if(flag){

                    mShopList.get(i).NUM++;

                }else{

                    if(mShopList.get(i).NUM==1){

                        mShopList.remove(i);

                    }

                    else{

                        mShopList.get(i).NUM--;

                    }

                }

            }

            mShopCarAdapter.notifyDataSetChanged();

            setPayMoney();

        }
    }

    private void setPayMoney(){

        if(mShopList.size()==0){

            ShopCount.SendCount(MaintainShopActivity.this,0);

            mShopCarImg.setBackgroundResource(R.mipmap.icon_maintain_car_empty);

            mShoppinpCartLeft.setBackgroundColor(getResources().getColor(R.color.MainColor2));

            mPayLin.setBackgroundColor(Color.parseColor("#dedede"));

            tv_confirm.setTextColor(Color.parseColor("#757575"));

            mMoneyText.setVisibility(View.GONE);

            mRel.setVisibility(View.GONE);

            mShopCarIsShow = false;

        }else{

            int num = 0;

            double money = 0;


            for(int i=0;i<mShopList.size();i++){

                num += mShopList.get(i).NUM;

                money += Double.parseDouble(mShopList.get(i).PRICE)*mShopList.get(i).NUM;

            }
            ShopCount.SendCount(MaintainShopActivity.this,num);

            mShopCarImg.setBackgroundResource(R.mipmap.icon_maintain_car_white);

            mShoppinpCartLeft.setBackgroundColor(getResources().getColor(R.color.MainColor));

            mPayLin.setBackgroundColor(getResources().getColor(R.color.ScollColor1));

            mMoneyText.setVisibility(View.VISIBLE);

            tv_confirm.setTextColor(Color.parseColor("#ffffff"));

            this.money = money + "";

            mMoneyText.setText("共计"+ Tools.F_num(this.money) + "元");

            mShopCarAdapter.notifyDataSetChanged();

            setHeight();

        }

    }

    private void setHeight(){

        if(mShopList.size()>=4){

            JTApplication.ScaleScreenHelper.loadViewWidthHeight(mShopCarListView,720,368);

        }else{

            JTApplication.ScaleScreenHelper.loadViewWidthHeight(mShopCarListView,720,92*mShopList.size());

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        ShopCount.SendCount(MaintainShopActivity.this,0);

    }

}
